package com.webchat.netty.server.service;

import com.webchat.common.util.IPAddressUtil;
import com.webchat.netty.server.common.NettyInstanceUtils;
import com.webchat.netty.server.config.properties.NettyServerProperties;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserChannelManager {

    public static Map<String, Channel> channels = new ConcurrentHashMap<>();

    private static ChannelRegistryService channelRegistryService = new ChannelRegistryService();

    public static void addChannel(String userId, Channel channel) {
        channels.put(userId, channel);
    }

    public static Channel getChannel(String userId) {
        return channels.get(userId);
    }

    public static void removeChannel(String userId) {
        channels.remove(userId);
    }

    public static String getInstance(String userId) {
        return channelRegistryService.getInstance(userId);
    }

    public static void registryInstance(String userId) {
        /**
         * 注册节点信息到redis
         */
        String instanceId = NettyInstanceUtils.getInstanceMd5();
        channelRegistryService.registry(userId, instanceId);
    }
}
